<?php

# Simple SIAKAD
# Aplikasi Sistem Informasi Akademik untuk PT/PTS Indonesia
# 
# version 1.0
# class set
# file controllers/set.php
# created Dec 29, 2012 6:48:13 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Set extends CI_Controller {

    var $data = array();

    public function __construct() {
        parent::__construct();

        $this->load->model('setting');        

        $this->load->model('authlogin');
        if (!$this->authlogin->valid_cookies()) {
            redirect('admin');
        } else {
            $this->data['sess'] = $this->authlogin->get_cookies();
        }
    }

    public function users() {
        
        $this->data['usr'] = $this->setting->get_users();
        
        $this->data['page'] = 'set_users';
        $this->load->view('dashboard', $this->data);
    }
    
    public function user_add(){
                
        $post = $this->input->post();
        if($post){
            if($this->setting->insert($post)){
                $this->data['msg'] = "Data Pengguna berhasil ditambahkan";
                $this->data['sts'] = true;
            }else{
                $this->data['msg'] = $this->setting->get_msg();
                $this->data['sts'] = false;
            }
        }
        
        $this->data['post'] = $post;
        $this->data['page'] = 'set_add_user';
        $this->load->view('dashboard', $this->data);
    }
    
    public function user_edit($id){
        
        $post = $this->input->post();
        if($post){
            if($this->setting->update($id,$post)){
                $this->data['msg'] = "Data Pengguna berhasil diubah";
                $this->data['sts'] = true;
            }else{
                $this->data['msg'] = $this->setting->get_msg();
                $this->data['sts'] = false;
            }
        }
        
        $this->data['usr'] = $this->setting->get_users($id);
        $this->data['page'] = 'set_edit_user';
        $this->load->view('dashboard', $this->data);
    }

    public function passwd() {

        $post = $this->input->post();
        $sess = $this->authlogin->get_cookies();
        if ($post) {
            if ($this->setting->change_pass($sess['id'], $post)) {
                $this->data['msg'] = "Ganti password berhasil";
                $this->data['sts'] = true;
            } else {
                $this->data['msg'] = $this->setting->get_msg();
                $this->data['sts'] = false;
            }
        }

        $this->data['page'] = 'set_passwd';
        $this->load->view('dashboard', $this->data);
    }
    
    public function reset_pass($id){
        $key = random_string('alnum', 8);
        $this->data['usr'] = $this->setting->get_users($id);
        $this->data['key'] = $key;
                
        $this->setting->force_chg_pass($id, $key);
        
        $this->data['page'] = 'reset_pass';
        $this->load->view('confirm', $this->data);
        
    }    

}

